<?php

namespace app\wx\controller;

use think\Db;
use think\facade\Request;
use think\Validate;
use think\helper\Time;

class Work extends Base
{
    protected $beforeActionList = [
        'first',
    ];
    protected $uid = '';

    protected function first()
    {
        if (session('uid') || input('openid')) {
            if (session('uid')) {
                $this->uid = session('uid');
            } else {
                $this->uid = DB::name('admin')->where('openid', input('openid'))->value('id');
                session('uid', $this->uid);
            }
        } else {
            $url = url('/wx/work/login', '', '', Request::host());
            $this->redirect('https://openwx.weibuyun.com/wxuser/get_openid' . '?domain=' . Request::host() . '&url=' . $url);
        }
    }


    public function login()
    {
        $info = DB::name('admin')->where('id', $this->uid)->find();
        // dump($info);
        if ($this->request->isPost() && input('?post.token')) {

            if (cache('login' . input('token') . 'time') > time()) {
                cache('login' . input('token'), $info['username'], 60 * 3);
                $this->success('授权成功，请到web中继续操作');
            } else {
                $this->error('超时，请重新扫码' . cache('login' . input('token') . 'time'));
            }
        }
        if ($info) {
            $info['face'] = "/wx" . trim($info['face']);
            //  dump($info);
            $this->assign('info', $info);
            $this->assign('token', input('token'));
            return view();
        } else {
            $this->assign('info', '没有找到你的用户信息,请先关注公众号');
            return view('work/error');
        }

    }


}
